import * as api from "@/api"
export default {
    namespaced: true,
    state: {
        name: "product模块",
        products: []
    },
    mutations: {
        //设置产品数据
        SET_PRODUCT(state, payload) {
            // console.log(payload)
            state.products = payload

            // console.log(state)
        },
        //减少库存
        DECREASE_INVENTORY(state, payload) {
            //根据传入的数据,查找products中是否存在,如果存在,数量-1
            let target = state.products.find(item => item.id === payload.id)
            // console.log(target)
            target.inventory--
        },
        //增加库存
        INCREASE_INVENTORY(state, payload) {
            let target = state.products.find(item => item.id === payload.id)
            // console.log(target)
            target.inventory++
        }
    },
    actions: {
        //获取购物车数据
        FETCH_PRODUCT({ commit, state }) {
            console.log('333', state.products)
            // if (state.products.length === 0) {
                // fetch("/api/data/index.json?data=v1")
                //     .then(body => body.json())
                //     .then(res => {
                //         // console.log(res)
                //         commit("SET_PRODUCT", res.data)
                //     })

                api.getProduct()
                .then(res=>{
                    console.log(res)
                    commit("SET_PRODUCT", res.data.data)
                })
                // console.log(api)
            // }
        }
    },
    getters: {},
}